<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initLogging() {
     #   ini_set("session.cookie_lifetime", 10000);
     #   ini_set("session.use_trans_sid", 1);
        $this->bootstrap('frontController');
        /*
          $front = Zend_Controller_Front::getInstance();
          $front->registerPlugin(new Big_Plugin_AdminContext());
          $front->dispatch();
         */

        $logger = new Zend_Log();
        $writer = 'production' == $this->getEnvironment() ?
             #   new Zend_Log_Writer_Firebug() :
                new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../logs/app.log') :
                new Zend_Log_Writer_Firebug();
        $logger->addWriter($writer);

        if ('production' == $this->getEnvironment()) {
            $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
            $logger->addFilter($filter);
        }

        $this->_logger = $logger;
        Zend_Registry::set('log', $logger);
    }

    protected function _initNoRoute() {
        #       $loader = new Zend_Loader_PluginLoader();
        #      $loader->addPrefixPath('Zend_Controller_Plugin_Noroute', APPLICATION_PATH . '/plugins');

        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Zend_Controller_Plugin_Noroute());
    }

    protected function _initAutoload() {
        #$this->_logger->info('Bootstrap ' . __METHOD__);
        $modelLoader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => APPLICATION_PATH));
        return $modelLoader;
    }

    protected function _initLayoutViewHelper() {
        #       $this->_logger->info('Bootstrap ' . __METHOD__);
        #	$this->bootstrap('view');
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        #	$view = $this->getResource('view');
        $view = $layout->getView();
        $layout->getView()->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Zend_View_Helper_Translatex');

        $view->doctype('HTML4_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=utf-8')
                ->appendName('Description', 'Gra');
        $view->headTitle('Grand Marnier');
        $view->headTitle()->setSeparator(' - ');
    }

    protected function _initDbProfiler() {
        #       $this->_logger->info('Bootstrap ' . __METHOD__);
        if ('production' !== $this->getEnvironment()) {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $this->getPluginResource('db')
                    ->getDbAdapter()
                    ->setProfiler($profiler);
        }
    }

    protected function _initSession() {
        #     $this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Session::start();
        //Zend_Registry::set('session', $value);
    }

    protected function _initSessionExpire() {
        #     $this->_logger->info('Bootstrap ' . __METHOD__);
        #   Zend_Registry::set('sessionExpire', 9);
        Zend_Registry::set('sessionExpire', 900);
        Zend_Registry::set('sessionAdminExpire', 900);
    }

    protected function _initLanguage() {
        $session = new Zend_Session_Namespace('GrandMarnier');
#        if ($session->userLanguage ) {
        #       } else {
        #          $session->userLanguage = 'en';
        #     }
        if ($this->getEnvironment() == 'production') {
            switch ($_SERVER['HTTP_HOST']) {
                case 'en.grand-marnier.com':
                    $session->userLanguage = 'en';
                    break;
                case 'fr.grand-marnier.com':
                    $session->userLanguage = 'fr';
                    break;
                case 'es.grand-marnier.com':
                    $session->userLanguage = 'sp';
                    break;
                default:
                    $session->userLanguage = 'en';
            }
        } elseif ($this->getEnvironment() == 'staging') {
            switch ($_SERVER['HTTP_HOST']) {
                case 'dev-en.grand-marnier.com':
                    $session->userLanguage = 'en';
                    break;
                case 'dev-fr.grand-marnier.com':
                    $session->userLanguage = 'fr';
                    break;
                case 'dev-es.grand-marnier.com':
                    $session->userLanguage = 'sp';
                    break;
                default:
                    $session->userLanguage = 'en';
/*                case 'def_en.grand-marnier.com':
                    $session->userLanguage = 'en';
                    break;
                case 'def_fr.grand-marnier.com':
                    $session->userLanguage = 'fr';
                    break;
                case 'def_es.grand-marnier.com':
                    $session->userLanguage = 'sp';
                    break;
                default:
                    $session->userLanguage = 'en';
 *
 */
            }
        } elseif ($this->getEnvironment() == 'development') {
            switch ($_SERVER['HTTP_HOST']) {
                default:
                    $session->userLanguage = 'en';

            }
        }
    }

    protected function _initTranslationCache() {
        $frontend = array(
            'lifetime' => 7200,
            'automatic_seralization' => true
        );
        $backend = array(
            'cache_dir' => '../application/zfcache/'
        );
        $cache = Zend_Cache::factory('Core', 'File', $frontend, $backend);
        Zend_Registry::set('translationCache', $cache);
    }

    protected function _initRouter() {
        /*

          $frontController = Zend_Controller_Front::getInstance();
          $router = $frontController->getRouter();

          $route = new Zend_Controller_Router_Route_Static (
          'login',
          array('controller' => 'accounts',
          'action' => 'login')
          );

          $router->addRoute('login', $route);
         *
         */
    }

    protected function _initGeoLocCountry() {
        $session = new Zend_Session_Namespace('GrandMarnier');
        $geoloc = $this->getGeoCountry();
        $session->domainRedLoc = $geoloc;
        $session->domainRedURL = $_SERVER['HTTP_HOST'];
    }

    private function getGeoCountry() {
        $session = new Zend_Session_Namespace('GrandMarnier');
        if ($session->geoLocation == "") {
            $this->bootstrap('db');
          #  $this->bootstrap('modules');
            $ipLite = new Application_Model_LocationByIP;
            $ipLite->setKey('fa2be0840f10980827eee41c6788a30dad8a680dda30d8b55de8f30528f17b9f');
            $geoLocDB = new Application_Model_DbTable_ZFGeoLoc();
            $nn = $ipLite->getOnlyCountryName($_SERVER['REMOTE_ADDR']);
            $locations = $session->geoLocation = $geoLocDB->getCountryCodeByGeo($nn);
            $errors = $ipLite->getError();
        } else {
            $locations = $session->geoLocation;
        }
        return $locations;
    }

    /*
      protected function _initRouter(){
      $this->_logger->info('Bootstrap ' . __METHOD__);

      $autoloader = Zend_Loader_Autoloader::getInstance();
      $autoloader->registerNamespace('Big');

      $router = new Zend_Controller_Router_Rewrite();
      $route = new Zend_Controller_Router_Route(
      'admin/:controller/:action/*',
      array(
      'action'	=> 'index',
      'controller'=>	'admin',
      'isAdmin'	=>	true,
      )
      );
      $router->addRoute('admin', $route);


      #require_once 'Zend/Loader.php';
      #	Zend_Loader::registerAutoload();

      }
     */
}

